package com.easy.util;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.codec.Base64;
import java.util.Map;
import java.util.UUID;

public class CaptchaUtil {
    private static final ExpiryMap expiryMap=new ExpiryMap();
    private static final long EXPIRY_TIME=1000*60*8;
    /**
     * 获取验证码
     * 验证验证码
     */
    public static Map getCaptchaUtil(){
//        LineCaptcha lineCaptcha = cn.hutool.captcha.CaptchaUtil.createLineCaptcha(200, 100,4,10);
        ShearCaptcha captcha = cn.hutool.captcha.CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        //图片

        String imgbase64 = Base64.encode(captcha.getImageBytes());
        System.out.println(imgbase64);
        //验证码中的编号
        String code = captcha.getCode();
        System.out.println(code);
        String key= UUID.randomUUID().toString();
        Map map=Map.of("key",key,"base","data:image/png;base64,"+imgbase64);
        expiryMap.put(key,code,EXPIRY_TIME);
        return map;
    };
    public static boolean checkCaptcha(String key,String code){
        String captchaCode =(String)expiryMap.get(key);
        return code.equalsIgnoreCase(captchaCode);
    };
}
